

package TagServer::Control;


#UML_MODELER_BEGIN_PERSONAL_VARS_Control

#UML_MODELER_END_PERSONAL_VARS_Control

use strict;
#@INC={"../"};

use TagServer::Fornecedor;

use base qw( TagServer::iServer );

=head1 NAME

Class Control implements iServer

=head1 SINOPSYS 

 use TagServer::Control;
  
 my %mv = (view  => TagClient::iView,
           model => TagServer::iMode)
	  );
 my $controler = new Control();
 %mv = $controler->onEvt("cadastra","fornecedor");

=head1 DESCRIPTION

 Control is the implementation of abstract class iServer.
 This is the controler of the TagFashion's MVC.
 When it receives an event "evt" of the "what" thing, it returns the specific model and view objects of types that can be implementations of that abstract classes, like Pessoa, Produto, Empresa and so on.

 evt can be:

=over

=item * cadastra 

=item * mostra 

=item * edita 

=item * deleta

=back

=head1 CLASS STRUCTURE

=head3 Public Attributs

=head4 _evt

	$ : The event to dispatch 

=head4 _what

        $ : What to dispatch

=head3 Public Methods


=head4 ->new()

   Return : 
	The class instance      

   Description : 
      The constructor

=cut

sub new
{
  warn "Construtor: sou Control\n";
  my $this = shift;
  my $class = ref($this) || $this; 
  my $self = {
  _evt  => undef,
  _what => undef,
  _view => undef,
  _model => undef,
  _dataHandle => undef,
  };
  bless ($self, $class); 
  return $self;
}

=head2 Acessors


=head3 Control->evt($new_evt)

   Parameters :
      new_evt : $ : The event

   Return : 
      $ : the _evt attrib 

   Description : 
     whith parameters sets the event to dispatch

=cut

sub evt {
   my $self = shift;
   if (@_) { $self->{_evt} = shift }
   return $self->{_evt};
}


=head3 Control->what($new_what) 

   Parameters :
      new_what : $ : what to dispatch

   Return : 
      $ : the _what attrib 

   Description : 
     whith parameters sets what to dispatch

=cut

sub what {
   my $self = shift;
   if (@_) { $self->{_what} = shift }
   return $self->{_what};
}

=head3 Control->view($new_view)

   Parameters :
      new_view : $ : The thew view object

   Return : 
      $ : the _view attrib 

   Description : 
     The view object that I can change

=cut

sub view {
   my $self = shift;
   if (@_) { $self->{_view} = shift }
   return $self->{_view};
}


=head3 Control->model($new_model)

   Parameters :
      new_model : $ : The event

   Return : 
      $ : the _model attrib, object at iModel interface 

   Description : 
     The model object that I can update

=cut

sub model {
   my $self = shift;
   if (@_) { $self->{_model} = shift }
   warn "Metodo Control->model\n";
   return $self->{_model};
}

=head3 Model->dataHandle($new_handle) 

   Parameters :
      new_handle : $ : the handle to DBMS

   Return : 
      $ : the handle

   Description : 
     this is a basic interface to any DBMS by the DBI.pm
     must be linked to Portal->model->dataHandle

=cut

sub dataHandle {
   my $self = shift;
   if (@_) { $self->{_what} = shift }
   return $self->{_what};
}




=head3 update

   Parameters :

      Return : 
            TagServer::iModel

	       Description : 
	             

=cut

sub update
{
  my $self = shift;
  # update a new model 
  if (@_) { $self->{_model} = shift }
  if ($self->view){$self->model->notify($self->view)}
  ##UML_MODELER_END_PERSONAL_CODE_update
}


=head3 change

   Parameters :

      Return : 
            TagClient::iView

	       Description : 
	             

=cut

sub change
{
  my($self    ) = @_;
  #UML_MODELER_BEGIN_PERSONAL_CODE_change
  ##UML_MODELER_END_PERSONAL_CODE_change
}



=head3 Control->onEvt($evt, $what)

   Parameters :
      evt : $ : The event
      what : $ : what to do

   Return : 
      % : the iModel and iView instances 

   Description : 
      Do what in that event

=cut

sub onEvt
{
  my($self,
     $evt, # $ : The event
     $what, # $ : what to do
    ) = @_;
    my %mv = (
	    _view  => $self->view,
	    _model => $self->model,
    );
    
    my $nothing;
    $self->what($what);
    $_ = $self->evt($evt);
     
     SWITCH: {
               /^cadastra/      && do {
                                   warn ("cadastrando..\n");
				   for ($self->what()){
					   /fornecedor/ && do {
						   warn "fornecedor\n";
						   $mv{_model} = new TagServer::Fornecedor();
						   $mv{_view} = $mv{_model}->observer();
						   last;
					   };
					   /cliente/ && do {
						   warn "cliente\n";
						   last;
					   }; 
					   /pessoa/ && do {
						   warn "pessoa\n";
						   last;
					   }; 
					   /produto/ && do {
						   warn "produto\n";
						   last;
					   };
 
              $nothing = 1;
				   }

                                   last SWITCH;
                              };

               /^edita/      && do {
                                   warn ("editando..\n");
				   for ($self->what()){
					   /fornecedor/ && do {
						   warn "fornecedor\n";
						   last;
					   };
					   /cliente/ && do {
						   warn "cliente\n";
						   last;
					   };
					   /pessoa/ && do {
						   warn "pessoa\n";
						   last;
					   }; 
					   /produto/ && do {
						   warn "produto\n";
						   last;
					   };
 
              $nothing = 1;
				   }

                                   last SWITCH;
                              };

               /^deleta/      && do {
                                   warn ("deletando..\n");
				   for ($self->what()){
					   /fornecedor/ && do {
						   warn "fornecedor\n";
						   last;
					   };
					   /cliente/ && do {
						   warn "cliente\n";
						   last;
					   };
					   /pessoa/ && do {
						   warn "pessoa\n";
						   last;
					   }; 
					   /produto/ && do {
						   warn "produto\n";
						   last;
					   };
 
              $nothing = 1;
				   }

                                   last SWITCH;
                               };
               /^mostra/      && do {
                                   warn ("mostrando..\n");
				   for ($self->what()){
					   /fornecedor/ && do {
						   warn "fornecedor\n";
						   last;
					   };
					   /cliente/ && do {
						   warn "cliente\n";
						   last;
					   };
					   /pessoa/ && do {
						   warn "pessoa\n";
						   last;
					   }; 
					   /produto/ && do {
						   warn "produto\n";
						   last;
					   };
 
              $nothing = 1;
				   }

                                   last SWITCH;
                               };
              $nothing = 1;
           }

	   if ($nothing) {warn "Oooops, wrong event/what\n"}


    return %mv;

}


=pod 

=head2 PRIVATE METHODS


=cut

#update and change a view 
sub update{}

sub change{}




return 1;
